//
//  CZNetworkTool.swift
//  05.08--weibo
//
//  Created by fennyYang on 16/5/14.
//  Copyright © 2016年 a. All rights reserved.
//

import UIKit

import AFNetworking

// 请求方法枚举
enum RequestMethod: String
{
     case GET = "GET"
     case POST = "POST"
}

class CZNetworkTool: NSObject
{
     //单例
     static let sharedInstance: CZNetworkTool = CZNetworkTool()

     //属性AFHttpSessionManager
     // https://api.weibo.com/oauth2/access_token
     // https://api.weibo.com/2/statuses/home_timeline.json
     private let afnManager: AFHTTPSessionManager =
     {
          let afn = AFHTTPSessionManager(baseURL: NSURL(string: "https://api.weibo.com/"))

          // 给响应的序列化器多添加支持的类型

          afn.responseSerializer.acceptableContentTypes?.insert("text/plain")

          return afn
     }()


     // 如果直接使用AFN的GET和POST方法,如果AFN改变了方法名或参数改变了,哪项目中直接使用AFN.GET和POST的地方都需要改变

     // 自己封装GET 和 POST方法,项目中要使用GET和POST就用我们封装好的,到时如果如果AFN改变了方法名或参数改变了,只需要改我们封装的方法

     /// 封装AFN的GET和POST,通过参数来确定使用GET还是POST
     func request(method: RequestMethod, URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {

             if method == RequestMethod.GET
              {
                  afnManager.GET(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
              }
             else
              {
                  afnManager.POST(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
              }

        }



}








































